package com.wuming.caiwu.controller;

import com.wuming.caiwu.core.BusinessException;
import com.wuming.caiwu.core.CommonResult;
import com.wuming.caiwu.core.JsonResult;
import com.wuming.caiwu.model.QsgBanner;
import com.wuming.caiwu.service.QsgBannerService;
import com.wuming.caiwu.util.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * Created by hanfei on 16/4/19.
 */
@Controller
@RequestMapping("/web/banner")
public class QsgBannerController extends BaseController {

    @Autowired
    private QsgBannerService bannerService;

    @RequestMapping(value = "/manageBanner", method = RequestMethod.GET)
    public String manageBanner() {
        return "/banner/manageQsgBanner";
    }

    @RequestMapping(value = "/addBanner", method = RequestMethod.GET)
    public String addBanner() {
        return "/banner/editQsgBanner";
    }

    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult list(@RequestParam(defaultValue = "0") int pageIndex, @RequestParam(defaultValue = "10") int pageSize){
        try {
            List<QsgBanner> banners = bannerService.getBannerByPage(pageIndex, pageSize);
            int total = bannerService.getTotal();
            return CommonResult.createSuccessResult(banners, total, "查询Banner信息成功.");
        } catch (BusinessException be) {
            logger.error("发生业务异常,异常信息:{}", be.getMessage());
            return CommonResult.createFailureResult("查询Banner信息失败.");
        } catch (Exception e) {
            logger.error("发生系统异常,异常信息:{}", e.getMessage());
            return CommonResult.createFailureResult("查询Banner信息失败.");
        }
    }

    @RequestMapping(value = "/editBanner/{id}", method = RequestMethod.GET)
    public String editStudent(Model model, @PathVariable Long id) {
        logger.info("准备修改Banner信息ID:{}", id);
        QsgBanner product = bannerService.getBannerById(id);
        model.addAttribute("banner", product);
        return "/banner/editQsgBanner";
    }

    @ResponseBody
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public JsonResult save(QsgBanner product) {
        try {
            if (null != product.getId()) {
                bannerService.modifyBanner(product);
            } else {
                bannerService.saveBanner(product);
            }
        } catch (Exception e) {
            logger.error("保存Banner信息失败,失败信息:{}", e.getMessage());
            return new JsonResult<Object>(null, "保存Banner基本信息失败", false);
        }
        return new JsonResult<Object>(product.getId(), "保存Banner基本信息成功.", true);
    }

    @ResponseBody
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public CommonResult delete(@PathVariable Long id) {
        logger.info("准备删除banner信息ID:{}", id);
        try {
            bannerService.removeBannerById(id);
            return CommonResult.createSuccessResult("删除banner信息成功.");
        } catch (BusinessException be) {
            logger.error("发生业务异常,异常信息:{}", be.getMessage());
            return CommonResult.createFailureResult("删除banner信息失败.");
        } catch (Exception e) {
            logger.error("发生系统异常,异常信息:{}", e.getMessage());
            return CommonResult.createFailureResult("删除banner信息失败.");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/uploadImg", method = RequestMethod.POST)
    public JsonResult uploadImg(MultipartFile file) {
        String imageUrl = OssUtil.upload(file, OssUtil.FileDirType.ARTICLE_IMAGES);;
        return new JsonResult<Object>(imageUrl, "上传图片信息成功.", true);
    }


}
